package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.DownOrUpDto;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import org.bouncycastle.asn1.DERTaggedObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.itheima.core.controller.AbstractCoreController;

/**
 * @description <p>自媒体图文内容信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmNewsController",tags = "自媒体图文内容信息")
@RestController
@RequestMapping("news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }
    /**
     * 发表文章：草稿、提交待审核
     * @param draft 是否为草稿
     * @param dtoSave
     * @return
     */
    @PostMapping("submit")
    public ResultVo submit(boolean draft, @RequestBody WmNewsDtoSave dtoSave){
        wmNewsService.submit(draft,dtoSave);
        return ResultVo.ok();
    }
    /**
     * 文章列表 分页查询
     * @param dto
     * @return
     */
    @PostMapping("list")
    public PageResultVo findPage(@RequestBody WmNewsPageReqDto dto){
        PageResultVo vo = wmNewsService.findPage(dto);
        return vo;
    }


    /**
     * 通过id查询文章
     * @param id
     * @return
     */
    @GetMapping("one/{id}")
    public ResultVo one(@PathVariable(value = "id") Long id){
         WmNews wmNews= wmNewsService.findById(id);
        return  ResultVo.ok(wmNews);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @GetMapping("del_news/{id}")
    public  ResultVo deleteById(@PathVariable(value = "id")Long id){
        wmNewsService.deleteById(id);
        return ResultVo.ok();
    }
    /**
     * 文章上下架处理
     * @param dto
     * @return
     */
    @PostMapping("down_or_up")
    public ResultVo downOrUp(@RequestBody @Validated DownOrUpDto dto){
        wmNewsService.downOruP(dto);
        return ResultVo.ok();
    }
}

